package com.ai.langchain4j.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.ByteBuffer;

import com.alibaba.dashscope.audio.ttsv2.SpeechSynthesisParam;
import com.alibaba.dashscope.audio.ttsv2.SpeechSynthesizer;
import dev.langchain4j.community.model.dashscope.WanxImageModel;
import dev.langchain4j.data.image.Image;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.output.Response;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: suren@517na.com
 * @CreateTime: 2025-05-25
 * @Description: Langchain4j整合ai模型 Controller
 */

@RestController
@RequestMapping("/v1/ai")
public class Langchain4jController {

    private static final String OPEN_AI_URL = "https://api.deepseek.com";
    private static final String API_KEY = "sk-1345efc9558d4ab49ff93c59910fafd6";

    private static final String ALI_API_KEY = "sk-2969f353969041998fa3b70b049bd5aa";


    @GetMapping("/openai")
    public String openAiChat(@RequestParam(defaultValue = "你好，你是谁？") String message) {
        ChatLanguageModel model = OpenAiChatModel
                .builder()
                .apiKey("demo")
                .modelName("gpt-4o-mini")
                .build();
        String chat = model.chat(message);
        return chat;
    }


    @GetMapping("/ds")
    public String deepSeek(@RequestParam(defaultValue = "你好，你是谁？") String message) {
        ChatLanguageModel model = OpenAiChatModel
                .builder()
                .baseUrl(OPEN_AI_URL)
                .apiKey(API_KEY)
                .modelName("deepseek-r1")
                .build();
        String answer = model.chat(message);
        return answer;
    }

    @GetMapping("/wensheng")
    public URI wenSheng(@RequestParam(defaultValue = "可爱的小狗") String message) {
        WanxImageModel model = WanxImageModel
                .builder()
                .modelName("wanx2.1-t2i-plus")
                .apiKey(ALI_API_KEY)
                .build();
        Response<Image> response = model.generate(message);
        return response.content().url();
    }


    @GetMapping("/yuyin")
    public void wenShengYuYin() {
        SpeechSynthesisParam param = SpeechSynthesisParam
                .builder()
                .apiKey(ALI_API_KEY)
                .model("cosyvoice-v1")
                .voice("longxiaochun")
                .build();

        SpeechSynthesizer synthesizer = new SpeechSynthesizer(param, null);
        ByteBuffer audio = synthesizer.call("大家好，我是龙玉虎");
        File file = new File("output.mp3");
        try (FileOutputStream fos = new FileOutputStream(file)){
            fos.write(audio.array());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
